package manager;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import entity.Bid;
import entity.Course;
import entity.Section;
import entity.Student;

/**
 * An utility class to execute read queries, handle result sets and obtain
 * results in the desired entity object type.
 * 
 * @author
 * 
 */
class RetrieveBidUtil {

	/**
	 * Convenient method to retrieve multiple bids from database for the given
	 * query.
	 * 
	 * @param dbMgr
	 *            database manager object on which the query should be executed
	 * @param sql
	 *            the SQL statement to execute
	 * @param param
	 *            sequence of parameters to be used in the SQL statement
	 * @return an ArrayList of bid objects with information retrieved from the
	 *         database
	 */
	static ArrayList<Bid> retrieveMultipleBidsForQuery(DatabaseMgr dbMgr,
			String sql, Object... param) {
		ArrayList<Bid> bids = new ArrayList<Bid>();

		try {
			ResultSet rs = dbMgr.openAndRead(sql, param);
			while (rs.next()) {
				bids.add(newBidFromResultSet(rs));
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return bids;
	}

	/**
	 * Convenient method to retrieve a single bid from database for the given
	 * query.
	 * 
	 * @param dbMgr
	 *            database manager object on which the query should be executed
	 * @param sql
	 *            the SQL statement to execute
	 * @param param
	 *            sequence of parameters to be used in the SQL statement
	 * @return a new bid object with information retrieved from the database
	 */
	static Bid retrieveBidForQuery(DatabaseMgr dbMgr, String sql,
			Object... param) {
		Bid bid = null;
		try {
			ResultSet rs = dbMgr.openAndRead(sql, param);
			while (rs.next()) {
				bid = newBidFromResultSet(rs);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return bid;
	}

	/**
	 * Convenient method to create new bid object from the given result set.
	 * 
	 * @param rs
	 *            result set containing bid information
	 * @return a new bid object with information extracted from the result set
	 * @throws SQLException
	 */
	private static Bid newBidFromResultSet(ResultSet rs) throws SQLException {
		Student student = new Student(rs.getString("userId"),
				rs.getString("password"), rs.getString("name"),
				rs.getString("studentSchool"), rs.getDouble("edollar"));

		Course course = new Course(rs.getString("courseCode"),
				rs.getString("title"), rs.getString("description"),
				rs.getString("courseSchool"), rs.getString("examDate"),
				rs.getString("examStartTime"), rs.getString("examEndTime"));

		Section section = new Section(course, rs.getString("sectionCode"),
				rs.getString("venue"), rs.getString("day"),
				rs.getString("startTime"), rs.getString("endTime"),
				rs.getString("instructor"), rs.getInt("size"));

		double bidAmount = rs.getDouble("bidAmount");

		try {
			String bidResult = rs.getString("bidResult");
			return new Bid(student, section, bidAmount, bidResult);

		} catch (SQLException e) {
			return new Bid(student, section, bidAmount);
		}

	}
}
